from django.db import models

# Create your models here.


from dvadmin.utils.models import CoreModel, SoftDeleteModel
from dvadmin.system.models import Dictionary, Dept


class ChallengeItem(CoreModel, SoftDeleteModel):
    """
    挑战类型表
    1、名称  销售金额挑战  利润率挑战
    2、关联字段
    3、成功判定   [(0, 小于等于),(1, 大于等于)]
    4、是否显示进度条
    """
    name = models.CharField(max_length=50, verbose_name="名称")
    field = models.CharField(max_length=50, verbose_name="关联key")
    success_judgment = models.IntegerField(choices=[(0, '小于等于'), (1, '大于等于')], verbose_name="成功判定")
    show_progress = models.BooleanField(default=True, verbose_name="是否显示进度条")
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '挑战项目'
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)


class DeptChallenge(CoreModel, SoftDeleteModel):
    """
    部门挑战
    1、 挑战部门
    2、挑战周期类型  周挑战  月挑战  年挑战
    3、挑战开始日期
    4、挑战结束日期
    5、挑战类型
    6、临界值
    """
    # 计算类型的选项
    CALCULATION_DATE_RANGE = [
        ("default", '挑战'),
        ("week", '周挑战'),
        ("month", '月挑战'),
        ("quarter", '季挑战'),
        ("year", '年挑战'),
    ]
    department = models.ForeignKey(to=Dept, on_delete=models.PROTECT, verbose_name="挑战部门")
    date_range = models.CharField(max_length=30, choices=CALCULATION_DATE_RANGE, verbose_name="挑战周期类型")
    start_date = models.DateField(verbose_name="挑战开始日期")
    end_date = models.DateField(verbose_name="挑战结束日期")
    challenge_type = models.ForeignKey(to=ChallengeItem, on_delete=models.PROTECT, verbose_name="挑战类型")
    value = models.FloatField(verbose_name="临界值")
    is_challenge = models.BooleanField(default=False, verbose_name="是否挑战")
    def __str__(self):
        return self.department.name
    class Meta:
        verbose_name = '部门挑战'
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)